import pygame
from src.manage.config import Settings

class Plane:
    """
    飞机类
    """
    def __init__(self, ai_game):
        # 初始化飞机,设置初始位置
        self.screen = ai_game.screen
        self.settings = Settings()
        self.screen_rect = ai_game.screen.get_rect()

        # 加载飞机图像并获取其外接矩形
        self.image = pygame.image.load(f"{Settings.img_path}plane.png")
        self.rect = self.image.get_rect()

        # 新飞船放在屏幕底部中央
        self.rect.midbottom = self.screen_rect.midbottom

        self.x = float(self.rect.x)
        self.y = float(self.rect.y)

        # 移动标志
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False
        self.dont_move = False


    def update(self):
        """
        根据标志调整飞船的位置
        """
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.x += self.settings.fly_speed
        if self.moving_left and self.rect.left > 0:
            self.x -= self.settings.fly_speed
        if self.moving_up and self.rect.top > self.screen_rect.top:
            self.y -= self.settings.fly_speed
        if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
            self.y += self.settings.fly_speed
        if self.dont_move:
            pass

        self.rect.x = self.x
        self.rect.y = self.y


    def blitme(self):
        """
        在指定位置绘制飞船
        """
        self.screen.blit(self.image, self.rect)

    def center_ship(self):
        self.rect.midbottom = self.screen_rect.midbottom
        self.x = float(self.rect.x)
